<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Contact extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->library('email');
        //$this->load->library('upload');
        $this->load->library('session');
        $this->load->model('Contact_model');
        $this->load->model('Function_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
    }
    public function index()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $sql="select * from Role";
        $query=$this->db->query($sql);
        $data['listemail']=$query->result();
        $this->load->view('_header',$data);
        $this->load->view('contact/loadFormContact');
        $this->load->view('_footer');
    }
    public function sendMail()
    {
        /*$config['upload_path'] = base_url().'templates/images/upload';
        $config['allowed_types'] = 'exe|psd|pdf|xls|ppt|php|rar|php4|php3|js|swf|Xhtml|zip|mid|midi|mp2|mp3|wav|bmp|gif|jpg|jpeg|png|html|htm|txt|rtf|mpeg|mpg|avi|doc|docx|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();*/
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $sql="select FullName from Role where email='$email'";
        $query=$this->db->query($sql);
        foreach($query->result() as $row)
        {
            $data['FullName']=$row->FullName;
            $FullName=$row->FullName;
        }
        //$file_name = $_FILES['file_upload']['name'];
            $To=trim($this->security->sanitize_filename($this->input->post('txtTo')));
            $Subject=trim($this->security->sanitize_filename($this->input->post('txtSubject')));
            $Content=trim($this->security->sanitize_filename($this->input->post('txtContent')));
            $this->email->set_newline("\r\n");
            $this->email->from($email, 'EVOLABLE Asia '.$FullName);
            $this->email->to($To);
            $this->email->subject($Subject);
            $this->email->message($Content);
            $file_tmp = isset($_FILES['file_upload']['tmp_name']) ? $_FILES['file_upload']['tmp_name'] : "";
            $folder_upload_in ="../HRMT/templates/images/upload/";
            $file_name = isset($_FILES['file_upload']['name']) ? $_FILES['file_upload']['name'] : "";
            $this->Function_model->uploadImage($_FILES['file_upload']);
            $this->email->attach($folder_upload_in.$file_name);
            $name=$folder_upload_in.$file_name;
            if($this->email->send())
            {
            $this->Contact_model->addMail($email,$FullName,$To,$Subject,$Content,$name);
            $data['upload']=$_FILES['file_upload']['name'];
            redirect('contact/listSendMail');
            }		
            else
            {
            $data['sendmail_error'] = "Send mail to ".$To." error";
            $this->load->view('_header',$data);
            $this->load->view('sendMailError'); 
            $this->load->view('_footer');
            }
    }
    public function listSendMail()
    {
            $email=$this->session->userdata('email');
            $data['email']=$email; 
            $start_row = $this->uri->segment(3);
            $per_page = 10;
            $config['base_url'] =base_url().'contact/listSendMail';
            $query=$this->Contact_model->getListSendMail($email);
            $config['total_rows'] = $query->num_rows();
            $data['total_rows']=$query->num_rows();
            $config['per_page'] = $per_page;
            $this->pagination->initialize($config);
            $data['pagination'] = $this->pagination->create_links();
            $data['title_content']="Manage Staff and Salary";
            $data['query'] = $this->Contact_model->getListSendMailLimited($email,$per_page,$start_row);
            $this->load->view('_header',$data);
            $this->load->view('contact/listSendMail'); 
            $this->load->view('_footer');
    }
    public function listInbox()
    {
            $email=$this->session->userdata('email');
            $data['email']=$email; 
            $start_row = $this->uri->segment(3);
            $per_page = 10;
            $config['base_url'] =base_url().'contact/listInbox';
            $query=$this->Contact_model->getListInbox($email);
            $config['total_rows'] = $query->num_rows();
            $data['total_rows']=$query->num_rows();
            $config['per_page'] = $per_page;
            $this->pagination->initialize($config);
            $data['pagination'] = $this->pagination->create_links();
            $data['title_content']="Manage Staff and Salary";
            $data['query'] = $this->Contact_model->getListInboxLimited($email,$per_page,$start_row);
            $this->load->view('_header',$data);
            $this->load->view('contact/listInbox'); 
            $this->load->view('_footer');
    }
    public function replyMail($Contact_ID)
    {
       $email=$this->session->userdata('email');
       $data['email']=$email;
       $data['Contact_ID']=$Contact_ID;
       $query=$this->Contact_model->getContactByContactID($Contact_ID);
       foreach($query->result() as $row)
       {
       $data['From']=$row->From;
       $data['Subject']=$row->Subject;
       $data['Content']=$row->Content;
       
       }
       $this->load->view('_header',$data);
       $this->load->view('contact/loadReplyMailForm');
       $this->load->view('_footer');
    }
    public function replyEmail()
    {
       $email=$this->session->userdata('email');
       $data['email']=$email;
       $sql="select FullName from Role where email='$email'";
        $query=$this->db->query($sql);
        foreach($query->result() as $row)
        {
            $data['FullName']=$row->FullName;
            $FullName=$row->FullName;
        }
        $To=trim($this->security->sanitize_filename($this->input->post('txtFrom')));
        $Subject=trim($this->security->sanitize_filename($this->input->post('txtSubject')));
        $Content=trim($this->security->sanitize_filename($this->input->post('txtContent')));
        $this->email->set_newline("\r\n");
            $this->email->from($email, 'EVOLABLE Asia '.$FullName);
            $this->email->to($To);
            $this->email->subject($Subject);
            $this->email->message($Content);
             $file_tmp = isset($_FILES['file_upload1']['tmp_name']) ? $_FILES['file_upload1']['tmp_name'] : "";
            $folder_upload_in ="../HRMT/templates/images/upload/";
            $file_name = isset($_FILES['file_upload1']['name']) ? $_FILES['file_upload1']['name'] : "";
            $this->Function_model->uploadImage($_FILES['file_upload1']);
            $this->email->attach($folder_upload_in.$file_name);
            $name=$folder_upload_in.$file_name;
            if($this->email->send())
            {
            $this->Contact_model->addMail($email,$FullName,$To,$Subject,$Content,$name);
            $data['upload']=$_FILES['file_upload']['name'];
            redirect('contact/listSendMail');
            }		
            else
            {
            $data['sendmail_error'] = "Send mail to ".$To." error";
            $this->load->view('_header',$data);
            $this->load->view('sendMailError'); 
            $this->load->view('_footer');
            }
    }
    public function deleteInboxMail($Contact_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->Contact_model->deleteMail($Contact_ID);
        
        redirect('contact/listInbox');
        
    }
    public function deleteSendMail($Contact_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->Contact_model->deleteMail($Contact_ID);
        redirect('contact/listSendMail');
        
    }
    public function deleteAllSendMail()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        //$checkbox=$this->security->sanitize_filename($this->input->post('checkbox'));
        $checkbox=$_POST['checkbox'];
        foreach($checkbox as $item)
        {
            $this->Contact_model->deleteMail($item);
        }
        redirect('contact/listSendMail');
        /*for($i=0;$i<  count($checkbox);$i++)
        {
            $del_contactid=$checkbox[$i];
            $this->Contact_model->deleteMail($del_contactid);
        }
        redirect('contact/listSendMail');*/
    }
    public function deleteAllInboxMail()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        //$checkbox=$this->security->sanitize_filename($this->input->post('checkbox'));
        $checkbox=$_POST['checkbox'];
        foreach($checkbox as $item)
        {
            $this->Contact_model->deleteMail($item);
        }
        redirect('contact/listInbox');
    }
}
?>
